"I/O" means Input and Output
📥 Input
Reading data from keyboard, file, network, etc.
Example: Reading from a file
📤 Output
Writing data to file, screen, network, etc.
Example: Writing to a file
Complete Guide with Examples and Best Practices
"I/O" means Input and Output
Reading data from keyboard, file, network, etc.
Example: Reading from a file
Writing data to file, screen, network, etc.
Example: Writing to a file
A stream is like a pipe or channel that flows data.
Used to read data
Used to write data
Java has 2 types of streams:
Stream Type | What it handles | Use for |
---|---|---|
Byte Stream | Handles binary data | Images, PDFs, audio |
Character Stream | Handles text data | Text files, strings |
Byte streams read/write 1 byte at a time.
import java.io.*; public class ReadByteExample { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("data.txt"); int i; while ((i = fis.read()) != -1) { System.out.print((char) i); // convert byte to char } fis.close(); } }
import java.io.*; public class WriteByteExample { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("data.txt"); String data = "Hello from Java!"; fos.write(data.getBytes()); // convert String to byte[] fos.close(); } }
Character streams read/write characters (2 bytes).
import java.io.*; public class ReadCharExample { public static void main(String[] args) throws Exception { FileReader fr = new FileReader("data.txt"); int i; while ((i = fr.read()) != -1) { System.out.print((char)i); } fr.close(); } }
import java.io.*; public class WriteCharExample { public static void main(String[] args) throws Exception { FileWriter fw = new FileWriter("data.txt"); fw.write("Welcome to Java I/O!"); fw.close(); } }
Buffered streams make input/output faster by reducing the number of system calls.
import java.io.*; public class BufferedReadExample { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("data.txt")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
Java has a File
class to handle files.
import java.io.*; public class CreateFileExample { public static void main(String[] args) throws IOException { File file = new File("demo.txt"); if (file.createNewFile()) { System.out.println("File created"); } else { System.out.println("File already exists"); } } }
import java.io.*; public class FileWriteExample { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("demo.txt"); fw.write("This is a file handling example."); fw.close(); } }
import java.io.*; import java.util.*; public class FileReadScanner { public static void main(String[] args) throws FileNotFoundException { File file = new File("demo.txt"); Scanner sc = new Scanner(file); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } sc.close(); } }
import java.io.*; public class DeleteFileExample { public static void main(String[] args) { File file = new File("demo.txt"); if (file.delete()) { System.out.println("File deleted"); } else { System.out.println("File not deleted"); } } }
Serialization = Saving object to file
import java.io.*; class Student implements Serializable { int id; String name; public Student(int id, String name) { this.id = id; this.name = name; } }
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser")); Student s = new Student(1, "Deepak"); oos.writeObject(s); oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.ser")); Student s = (Student) ois.readObject(); System.out.println(s.id + " " + s.name); ois.close();
Task | Class Used |
---|---|
Read file (bytes) | FileInputStream |
Write file (bytes) | FileOutputStream |
Read file (chars) | FileReader |
Write file (chars) | FileWriter |
Fast reading | BufferedReader |
Create/Delete File | File |
Read file line-wise | Scanner |
Save object | ObjectOutputStream |
Load object | ObjectInputStream |
• FileReader reads char by char.
• BufferedReader reads full line, faster.
• Saving an object's state into a file.
• Byte stream → binary data
• Character stream → text data
• Use BufferedReader